﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

namespace BootyShaker
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Completed +=
            new EventHandler<PhotoResult>(PhotoChooserTaskCompleted);
            cameraCaptureTask = new CameraCaptureTask();
            cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCapture_Completed);
        }

        //// Simple button Click event handler to take us to the second page
        //private void Button_Click(object sender, RoutedEventArgs e)
        //{
        //    NavigationService.Navigate(new Uri("/GamePage.xaml", UriKind.Relative));
        //}



        private PhotoChooserTask photoChooserTask;
        CameraCaptureTask cameraCaptureTask;

        private void button1_Click(object sender, RoutedEventArgs e)
        {
           

        }

        void cameraCapture_Completed(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                BitmapImage bitmap = new BitmapImage();
                bitmap.SetSource(e.ChosenPhoto);
                WriteableBitmap picLibraryImage = new WriteableBitmap(bitmap);

                App.ShakeImage = picLibraryImage;

                Dispatcher.BeginInvoke(() =>
                {
                    this.NavigationService.Navigate(new Uri("/ShakeDesigner.xaml", UriKind.Relative));
                });
            }
        }



        private void PhotoChooserTaskCompleted(object sender, PhotoResult e)
        {
            if (e.TaskResult == TaskResult.OK)
            {
                BitmapImage bitmap = new BitmapImage();
                bitmap.SetSource(e.ChosenPhoto);
                WriteableBitmap picLibraryImage = new WriteableBitmap(bitmap);

                App.ShakeImage = picLibraryImage;

                Dispatcher.BeginInvoke(() =>
                {
                    this.NavigationService.Navigate(new Uri("/ShakeDesigner.xaml", UriKind.Relative));
                });
            }

        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            HubTileService.UnfreezeGroup("Food");
            // System.Windows.Input.Touch.FrameReported += new TouchFrameEventHandler(Touch_FrameReported);
        }
        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            HubTileService.FreezeGroup("Food");
            // System.Windows.Input.Touch.FrameReported -= Touch_FrameReported;
        }
        private void HubTile_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            HubTileService.FreezeGroup("Food");
            
           
            cameraCaptureTask.Show();
           
        }

        private void HubTile_Tap_1(object sender, System.Windows.Input.GestureEventArgs e)
        {
            HubTileService.FreezeGroup("Food");
            photoChooserTask.Show();
        }
    }
}